import { Component, EventEmitter, Input, OnDestroy, OnInit, Output, ViewEncapsulation } from '@angular/core';
import { Subscription } from 'rxjs';
import { ApesMessageService } from '@shared/apes/message';
import { UserAuthService } from '@core/services/user-auth.service';
import { AssemblyDrawingViewService } from '@core/services/assemblyDrawing/assemblyDrawing.view.service';

@Component({
  selector: 'cad-parts-list',
  templateUrl: './cad-parts-list.component.html',
  styleUrls: ['./cad-parts-list.component.scss'],
  encapsulation: ViewEncapsulation.None,
})

export class CadPartsListComponent implements OnInit, OnDestroy {

  private subscription: Subscription;
  private loadingSubscription: Subscription;
  private selectPartsSubscription: Subscription;
  @Output() selected = new EventEmitter();
  @Output() errorParts = new EventEmitter();
  oldSelect: any;
  listOfData = [];
  isLoading: boolean = false;
  @Input() publicPage: boolean =false;
  @Input() shoppingData : any[] = [];

  constructor(
    private msg: ApesMessageService,
    private userAuthService: UserAuthService,
    private assemblyDrawingViewService: AssemblyDrawingViewService,
  ) {

  }

  ngOnInit(): void {
    this.subscription = this.assemblyDrawingViewService.partsListChange$.subscribe((data: any) => {
      this.isLoading = false;
      if (data && data.length != 0 && Object.keys(data).length != 0) {
        data.forEach(item => {
          item.orderQuantity = 1;
          // item.expand = false;
        });
        this.listOfData = data;
        // console.log('查询出来的配件为：', data);
      } else {
        this.listOfData = [];
      }
      this.correctionList = [];
    });

    this.loadingSubscription = this.assemblyDrawingViewService.partsListLoadingChange$.subscribe((data: any) => {
      this.isLoading = data;
    });

    this.selectPartsSubscription = this.assemblyDrawingViewService.selectPartImg$.subscribe((data: any) => {
      // console.log('选中锚点',data);
      if (this.oldSelect != data) {
        this.oldSelect = data;
        this.checkPart(data);
      }
    });
  }

  ngOnDestroy(): void {
    this.subscription.unsubscribe();
    this.loadingSubscription.unsubscribe();
    this.selectPartsSubscription.unsubscribe();
  }

  orderParts(e, data) {
    e.stopPropagation();
    // console.log('添加进购物车', data);
    for (let i = 0; i < this.listOfData.length; i++) {
      let listData = this.listOfData[i];
      if (listData.imageSN && listData.imageSN == data.imageSN && listData.partCode != data.partCode) {
        // this.msg.info('该配件存在高低配，请注意选件',{apesDuration: 3000});
        data['highLowconfig'] = true;
        break;
      }
    }
    // this.correctionList.forEach((list)=>{
    //   if(list.partCode == data.partCode){
    //     data['parts'] = [];
    //   }
    // });
    // if(data.parts.length == 0){
    //   data['subGroupName'] = '';
    // }

    this.selected.emit(data);
  }

  isVisible: boolean = false;
  remark: any = '';
  correctionData: any;
  correctionList: any[] = [];

  handleCancel(){
    this.isVisible = false;
    this.correctionData = null;
    this.remark = '';
  }

  handleOk() {
    // console.log(this.remark);
    let data = {
      ...this.correctionData,
      remark:this.remark
    }
    this.isVisible = false;
    this.correctionData['parts'] = [];
    this.correctionList.push(data);
    this.errorParts.emit(data);
    this.remark = '';
    this.correctionData = null;
  }

  errorCorrection(e, data) {
    e.stopPropagation();
    this.isVisible = true;
    this.correctionData = data;
  }

  // checkError(data){
  //   let check = false;
  //   if(data.hasOwnProperty('partCode')){
  //     this.correctionList.forEach((list)=>{
  //       if(list.partCode == data.partCode){
  //         check = true;
  //       }
  //     });
  //   }
  //
  //   return check;
  // }

  showPartsName(value) {
    let name = '';
    if (value && value.length != 0) {
      if (value.length == 1) {
        name = value[0]['PJMC'];
      } else {
        for (let i = 0; i < value.length; i++) {
          if (i == value.length - 1) {
            name += value[i]['PJMC'];
          } else {
            name += (value[i]['PJMC'] + '; ');
          }
        }
      }
    }
    return name;
  }

  filterName(value) {
    let name = '';
    if (value) {
      name = value.replace(/\$\$/g, ';');
    }
    return name;
  }

  filterSN(value) {
    let name = '';
    if (value) {
      name = value.replace(/[()]/g, '');
    }
    return name;
  }

  checkPart(sn) {
    if(!sn) return;
    let _sn = sn.replace(/[()]/g, '');
    this.listOfData.forEach((list) => {
      if (list['imageSN']) {
        let lsn = list['imageSN'].replace(/[()]/g, '');
        if (lsn == _sn) {
          list['activate'] = true;
        } else {
          list['activate'] = false;
        }
      }
    });
    this.assemblyDrawingViewService.selectPart$.next(sn);
  }

  checkNotes(e, data) {
    e.stopPropagation();
    console.log(data);
  }

  checkShopData(param){
    // console.log(param);
    let num  = 0
    if(this.shoppingData.length != 0){
      this.shoppingData.forEach((shopData)=>{
        if(param.id == shopData.id){
          num = shopData.orderQuantity;
        }
      })
    }
    return num;
  }

}


